﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using MichaelKappel.Net.CSharp;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;

namespace phudieuhungthinh.Controls
{
    public partial class ProductLayoutControl : UserControl
    {
        private const string Key = "HomeLinks";

        protected void Page_Load(object sender, EventArgs e)
        {
            var productHtml = HelperCache.Get<string>(Key);
            if (productHtml == null)
                lock (this)
                {
                    productHtml = LayoutProductSlider();
                    HelperCache.Add(Key, productHtml, HelperCache.CachingTime.ShortTermSliding);
                }

            Controls.Add(new LiteralControl(productHtml));
        }

        private IList<ProductModel> LoadProductList()
        {
            IList<ProductModel> products = new List<ProductModel>();
            return products;
        }

        protected string LayoutProductSlider()
        {
            var html = new StringBuilder();
            var im = new LinkImpl();
            List<LinkModel> links = im.GetHomeLinks();

            html.Append("<div id='product-slider'>");
            html.Append("<button class='nav prev'></button>");
            html.Append("<button class='nav next'></button>");
            html.Append("<div id='list'>");
            html.Append("<ul>");

            foreach (LinkModel t in links)
            {
                html.Append("<li class='item'>");
                html.AppendFormat("<a href='/san-pham/{0}' class='overlay'></a>", t.Url);
                html.Append("<div class='img'>");
                if (t.ImageUrl != null)
                {
                    // /Images/Products/thumb/
                    html.AppendFormat("<img src='/hinh-anh/phu-dieu/{0}' alt='{1}' />", t.Url,
                        t.Title);
                }
                html.Append("</div>");
                html.AppendFormat("<div class='title'><span>{0}</span></div>", t.Title);
                html.Append("</li>");
            }
            html.Append("</ul>");
            html.Append("</div>");
            html.Append("</div>");

            return html.ToString();
        }
    }
}